using System;
using System.Collections.Generic;

// 事件参数基类
public abstract class EventArgs
{
    public object Sender { get; set; }
    public DateTime Timestamp { get; set; }

    protected EventArgs(object sender = null)
    {
        Sender = sender;
        Timestamp = DateTime.Now;
    }
}

// 事件监听器管理类
public class EventListenerManager : IDisposable
{
    private readonly EventDispatcher _dispatcher;
    private readonly Dictionary<Type, List<Delegate>> _registeredHandlers = new Dictionary<Type, List<Delegate>>();

    public EventListenerManager(EventDispatcher dispatcher)
    {
        _dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
    }

    // 注册事件监听器
    public void RegisterEvent<T>(Action<T> handler) where T : EventArgs
    {
        var eventType = typeof(T);
        
        if (!_registeredHandlers.ContainsKey(eventType))
        {
            _registeredHandlers[eventType] = new List<Delegate>();
        }
        
        _registeredHandlers[eventType].Add(handler);
        _dispatcher.RegisterEvent(handler);
    }

    // 批量注册事件监听器
    public void RegisterEvents(params (Type eventType, Delegate handler)[] events)
    {
        foreach (var (eventType, handler) in events)
        {
            if (!_registeredHandlers.ContainsKey(eventType))
            {
                _registeredHandlers[eventType] = new List<Delegate>();
            }
            
            _registeredHandlers[eventType].Add(handler);
            
            // 使用反射调用泛型方法
            var registerMethod = typeof(EventDispatcher).GetMethod("RegisterEvent").MakeGenericMethod(eventType);
            registerMethod.Invoke(_dispatcher, new object[] { handler });
        }
    }

    // 移除特定事件监听器
    public void UnregisterEvent<T>(Action<T> handler) where T : EventArgs
    {
        var eventType = typeof(T);
        
        if (_registeredHandlers.ContainsKey(eventType))
        {
            _registeredHandlers[eventType].Remove(handler);
            _dispatcher.RemoveEvent(handler);
        }
    }

    // 移除所有事件监听器
    public void UnregisterAllEvents()
    {
        foreach (var kvp in _registeredHandlers)
        {
            var eventType = kvp.Key;
            var handlers = kvp.Value;
            
            foreach (var handler in handlers)
            {
                var removeMethod = typeof(EventDispatcher).GetMethod("RemoveEvent").MakeGenericMethod(eventType);
                removeMethod.Invoke(_dispatcher, new object[] { handler });
            }
        }
        
        _registeredHandlers.Clear();
    }

    // 获取已注册的事件类型
    public IEnumerable<Type> GetRegisteredEventTypes()
    {
        return _registeredHandlers.Keys;
    }

    // 获取特定事件类型的处理器数量
    public int GetHandlerCount<T>() where T : EventArgs
    {
        var eventType = typeof(T);
        return _registeredHandlers.ContainsKey(eventType) ? _registeredHandlers[eventType].Count : 0;
    }

    // 实现IDisposable接口，自动清理
    public void Dispose()
    {
        UnregisterAllEvents();
    }
}

public class EventDispatcher
{
    // 使用类型作为key的事件字典
    private Dictionary<Type, object> _eventMap = new Dictionary<Type, object>();

    // 注册事件 - 使用类型作为key
    public Action<T> RegisterEvent<T>(Action<T> action) where T : EventArgs
    {
        var eventType = typeof(T);
        
        if (!_eventMap.ContainsKey(eventType))
        {
            _eventMap[eventType] = new List<Action<T>>();
        }
        
        var handlers = (List<Action<T>>)_eventMap[eventType];
        handlers.Add(action);
        
        return action;
    }

    // 移除事件 - 使用类型作为key
    public void RemoveEvent<T>(Action<T> action) where T : EventArgs
    {
        var eventType = typeof(T);
        
        if (_eventMap.ContainsKey(eventType))
        {
            var handlers = (List<Action<T>>)_eventMap[eventType];
            handlers.Remove(action);
            
            // 如果没有处理器了，移除整个事件
            if (handlers.Count == 0)
            {
                _eventMap.Remove(eventType);
            }
        }
    }

    // 触发事件 - 使用类型作为key
    public void DispatchEvent<T>(T args) where T : EventArgs
    {
        var eventType = typeof(T);
        
        if (_eventMap.ContainsKey(eventType))
        {
            var handlers = (List<Action<T>>)_eventMap[eventType];
            foreach (var handler in handlers)
            {
                handler?.Invoke(args);
            }
        }
    }

    // 检查事件是否存在
    public bool HasEvent<T>() where T : EventArgs
    {
        var eventType = typeof(T);
        return _eventMap.ContainsKey(eventType) && 
               _eventMap[eventType] is List<Action<T>> handlers && 
               handlers.Count > 0;
    }

    // 获取事件处理器数量
    public int GetEventCount<T>() where T : EventArgs
    {
        var eventType = typeof(T);
        if (_eventMap.ContainsKey(eventType) && _eventMap[eventType] is List<Action<T>> handlers)
        {
            return handlers.Count;
        }
        return 0;
    }

    // 清空所有事件
    public void Clear()
    {
        _eventMap.Clear();
    }

    // 清空特定事件
    public void ClearEvent<T>() where T : EventArgs
    {
        var eventType = typeof(T);
        if (_eventMap.ContainsKey(eventType))
        {
            _eventMap.Remove(eventType);
        }
    }

    // 获取所有已注册的事件类型
    public IEnumerable<Type> GetRegisteredEventTypes()
    {
        return _eventMap.Keys;
    }

    // 创建事件监听器管理器
    public EventListenerManager CreateListenerManager()
    {
        return new EventListenerManager(this);
    }
}